<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ page isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="ujn" uri="http://ujn.edu.cn/common/"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户管理</title>
<!-- 引入css样式文件 -->
<!-- Bootstrap Core CSS -->
<link href="<%=basePath%>css/bootstrap.min.css" rel="stylesheet" />
<!-- MetisMenu CSS -->
<link href="<%=basePath%>css/metisMenu.min.css" rel="stylesheet" />
<!-- DataTables CSS -->
<link href="<%=basePath%>css/dataTables.bootstrap.css" rel="stylesheet" />
<!-- Custom CSS -->
<link href="<%=basePath%>css/sb-admin-2.css" rel="stylesheet" />
<!-- Custom Fonts -->
<link href="<%=basePath%>css/font-awesome.min.css" rel="stylesheet"
	type="text/css" />
<link href="<%=basePath%>css/boot-crm.css" rel="stylesheet"
	type="text/css" />
</head>
<body>
	<div id="wrapper">
		<!-- 导航栏部分 -->
		<nav class="navbar navbar-default navbar-static-top" role="navigation"
			style="margin-bottom: 0">
			<div class="navbar-header">
				<a class="navbar-brand" href="<%=basePath%>user/list.action">客户管理系统</a>
			</div>
			<!-- 导航栏右侧图标部分 -->
			<ul class="nav navbar-top-links navbar-right">
				
				
				<!-- 用户信息和系统设置 start -->
				<li class="dropdown"><a class="dropdown-toggle"
					data-toggle="dropdown" href="#"> <i class="fa fa-user fa-fw"></i>
						<i class="fa fa-caret-down"></i>
				</a>
					<ul class="dropdown-menu dropdown-user">
						<li><a href="#"><i class="fa fa-user fa-fw"></i>
								用户：${USER_SESSION.user_name} </a></li>
						<li><a href="#"><i class="fa fa-gear fa-fw"></i> 系统设置</a></li>
						<li class="divider"></li>
						<li><a
							href="${pageContext.request.contextPath }/logout.action"> <i
								class="fa fa-sign-out fa-fw"></i>退出登录
						</a></li>
					</ul></li>
				<!-- 用户信息和系统设置结束 -->
			</ul>
			<!-- 左侧显示列表部分 start-->
			<div class="navbar-default sidebar" role="navigation">
				<div class="sidebar-nav navbar-collapse">
					<ul class="nav" id="side-menu">
						<li class="sidebar-search">
							<div class="input-group custom-search-form">
								<input type="text" class="form-control" placeholder="查询内容...">
								<span class="input-group-btn">
									<button class="btn btn-default" type="button">
										<i class="fa fa-search" style="padding: 3px 0 3px 0;"></i>
									</button>
								</span>
							</div>
						</li>
						<li><a
							href="${pageContext.request.contextPath }/customer/list.action">
								<i class="fa fa-edit fa-fw" data-id="customer"></i> 客户管理
						</a></li>
						<li><a
							href="${pageContext.request.contextPath }/dict/list.action">
								<i class="fa fa-book fa-fw" data-id="dict"></i> 字典维护
						</a></li>
						<li><a
							href="${pageContext.request.contextPath }/user/list.action">
								<i class="fa fa-pencil fa-fw" data-id="dict"></i> 用户管理
						</a></li>
					</ul>
				</div>
			</div>
			<!-- 左侧显示列表部分 end-->
		</nav>
		<!-- 用户列表查询部分  start-->
		<div id="content">
			<div id="page-wrapper">
				<div class="row">
					<div class="col-lg-12">
						<h1 class="page-header">用户管理</h1>
					</div>
					<!-- /.col-lg-12 -->
				</div>
				<!-- /.row -->
				<div class="panel panel-default">
					<div class="panel-body">
						<form class="form-inline" method="get"
							action="${pageContext.request.contextPath }/user/list.action">
							<div class="form-group">
								<label for="keyWord">模糊查询</label>
								 <input type="text"
									class="form-control" id="keyWord" value="${keyWord}"
									name="keyWord" />
							</div>
							<button type="submit" class="btn btn-primary">查询</button>
						</form>
					</div>
				</div>
				<a href="#" class="btn btn-primary" data-toggle="modal"
					data-target="#newUserDialog" onclick="clearUser()">新建</a>
				<div class="row">
					<div class="col-lg-12">
						<div class="panel panel-default">
							<div class="panel-heading">用户信息列表</div>
							<!-- /.panel-heading -->
							<table class="table table-bordered table-striped">
								<thead>
									<tr>
										<th>用户id</th>
										<th>用户账号</th>
										<th>用户名称</th>
										<th>用户密码</th>
										<th>是否可用</th>
									</tr>
								</thead>
								<tbody>
									<c:forEach items="${page.rows}" var="row">
										<tr>
											<td>${row.user_id}</td>
											<td>${row.user_code}</td>
											<td>${row.user_name}</td>
											<td>${row.user_password}</td>
											<td>${row.user_state}</td>
											<td><a href="#" class="btn btn-primary btn-xs"
												data-toggle="modal" data-target="#customerEditDialog"
												onclick="editUser(${row.user_id})">修改</a> <a href="#"
												class="btn btn-danger btn-xs"
												onclick="deleteUser(${row.user_id})">删除</a></td>
										</tr>
									</c:forEach>
								</tbody>
							</table>
							<div class="col-md-12 text-right">
								<ujn:page
									url="${pageContext.request.contextPath }/user/list.action" />
							</div>
							<!-- /.panel-body -->
						</div>
						<!-- /.panel -->
					</div>
					<!-- /.col-lg-12 -->
				</div>
			</div>
			<!-- 用户列表查询部分  end-->
		</div>
	</div>
	<!-- 创建用户模态框 -->
	<div class="modal fade" id="newUserDialog" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title" id="myModalLabel">新建用户信息</h4>
				</div>
				<div class="modal-body">
					<form class="form-horizontal" id="new_user_form">
						
						<div class="form-group">
							<label for="new_user_code" class="col-sm-2 control-label">用户账号</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="new_user_code"
									placeholder="用户账号" name="user_code" onblur="isExistByUserCode()" />
							</div>
						</div>
						<div class="form-group">
							<label for="new_user_name" class="col-sm-2 control-label">用户名称</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="new_user_name"
									placeholder="用户名称" name="user_name" />
							</div>
						</div>
						<div class="form-group">
							<label for="new_user_password" class="col-sm-2 control-label">用户密码</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="new_user_password" onblur="isExistPwd()"
									placeholder="用户密码" name="user_password" />
							</div>
						</div>
						<div class="form-group">
							<label for="new_user_state"
								style="float: left; padding: 7px 15px 0 27px;">是否可用</label>
							<div class="col-sm-10">
								<select class="form-control" id="new_user_state"
									name="user_state">
									<option value="1" selected>是</option>
									<option value="0">否</option>
								</select>
							</div>
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary" id="but_create_user"
						onclick="createUser()">创建用户</button>
				</div>
			</div>
		</div>
	</div>
	<!-- 修改用户模态框 -->
	<div class="modal fade" id="customerEditDialog" tabindex="-1"
		role="dialog" aria-labelledby="myModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title" id="myModalLabel">修改用户信息</h4>
				</div>
				<div class="modal-body">
					<form class="form-horizontal" id="edit_user_form">
						<div class="form-group">
							<label for="edit_user_id" class="col-sm-2 control-label">用户id</label>
							<div class="col-sm-10">
							<input type="text" class="form-control" id="p_user_id" disabled="disabled"
									placeholder="用户id" />
								<input type="hidden" id="edit_user_id" name="user_id" />
							</div>
						</div>
						<div class="form-group">
							<label for="edit_user_code" class="col-sm-2 control-label">用户账号</label>
							<div class="col-sm-10">
							<input type="text" class="form-control" id="p_user_code" disabled="disabled" 
									placeholder="用户账号" />
								<input type="hidden" id="edit_user_code" name="user_code" />
							</div>
						</div>
						<div class="form-group">
							<label for="edit_user_name" class="col-sm-2 control-label">用户名称</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="edit_user_name"
									placeholder="用户名称" name="user_name" />
							</div>
						</div>
						<div class="form-group">
							<label for="edit_user_password" class="col-sm-2 control-label">用户密码</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="edit_user_password" onblur="ExistEditPwd()"
									placeholder="用户密码" name="user_password" />
							</div>
						</div>
						<div class="form-group">
							<label for="edit_user_state"
								style="float: left; padding: 7px 15px 0 27px;">是否可用</label>
							<div class="col-sm-10">
								<select class="form-control" id="edit_user_state"
									name="user_state">
									<option value="1" selected>是</option>
									<option value="0">否</option>
								</select>
							</div>
						</div>
						<input type="hidden"  id="edit_pwd"	name="pwd" />
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary" id="but_edit"
						onclick="updateUser()">保存修改</button>
				</div>
			</div>
		</div>
	</div>
	<!-- 引入js文件 -->
	<!-- jQuery -->
	<script src="<%=basePath%>js/jquery-1.11.3.min.js"></script>
	<!-- Bootstrap Core JavaScript -->
	<script src="<%=basePath%>js/bootstrap.min.js"></script>
	<!-- Metis Menu Plugin JavaScript -->
	<script src="<%=basePath%>js/metisMenu.min.js"></script>
	<!-- DataTables JavaScript -->
	<script src="<%=basePath%>js/jquery.dataTables.min.js"></script>
	<script src="<%=basePath%>js/dataTables.bootstrap.min.js"></script>
	<!-- Custom Theme JavaScript -->
	<script src="<%=basePath%>js/sb-admin-2.js"></script>
	<!-- 编写js代码 -->
	<script type="text/javascript">
	function isExistByUserCode(){
		 $.ajax({
		        type:"get",
		        url:"<%=basePath%>user/isExistByUserCode.action",
		        data:{"user_code":$("#new_user_code").val()},
		        success:function(data) {
		        	 if(data =="OK"){
		        		 $("#but_create_user").attr("disabled",false);	 
		 	        }else if(data=="empty"){
		 	        	 $("#but_create_user").attr("disabled",true);
		 	        	  alert("账号"+data);
		 	        }else{
		 	        	 $("#but_create_user").attr("disabled",true);
		 	        	 alert("账号"+data);
		 	        }

		        }
		    });
	}
	function isExistPwd(){
		if($("#new_user_password").val()==null||$("#new_user_password").val()==""){
			 $("#but_edit").attr("disabled",true);
			 alert("密码不能为空！");
		}else{
			$("#but_edit").attr("disabled",false);
		}
	}
	function ExistEditPwd(){
		if($("#edit_user_password").val()==null||$("#edit_user_password").val()==""){
			 $("#but_create_user").attr("disabled",true);
			 alert("密码不能为空！");
		}else{
			$("#but_create_user").attr("disabled",false);
		}
	}
	//清空新建用户窗口中的数据
	function clearUser() {
	    $("#new_user_id").val("");
	    $("#new_user_code").val("")
	    $("#new_user_name").val("")
	    $("#new_user_password").val("")
	    $("#new_pwd").val("")
	}
	// 创建用户
	function createUser() {
	$.post("<%=basePath%>user/create.action",
	$("#new_user_form").serialize(),function(data){
	        if(data =="OK"){
	            alert("用户创建成功！");
	            window.location.reload();
	        }else{
	            alert("用户创建失败！");
	            window.location.reload();
	        }
	    });
	}
	// 通过id获取修改的用户信息
	function editUser(id) {
	    $.ajax({
	        type:"get",
	        url:"<%=basePath%>user/getUserById.action",
	        data:{"id":id},
	        success:function(data) {
	            $("#p_user_id").val(data.user_id);
	            $("#p_user_code").val(data.user_code);
	            
	            $("#edit_user_id").val(data.user_id);
	            $("#edit_user_code").val(data.user_code);
	            $("#edit_user_name").val(data.user_name);
	            $("#edit_user_password").val(data.user_password);
	            $("#edit_user_state").val(data.user_state);
	            $("#edit_pwd").val(data.user_password);
	
	        }
	    });
	}
    // 执行修改用户操作
	function updateUser() {
		var pwd0=  $("#edit_pwd").val();
		var pwd=  $("#edit_user_password").val();
		if(pwd0==pwd){//密码如果不更改的时候，不需要进行加密
			$("#edit_pwd").val("0");
		}else{
			$("#edit_pwd").val("1");
		}
		$.post("<%=basePath%>user/update.action",$("#edit_user_form").serialize(),function(data){
			if(data =="OK"){
				alert("用户信息更新成功！");
				window.location.reload();
			}else{
				alert("用户信息更新失败！");
				window.location.reload();
			}
		});
	}
	// 删除用户
	function deleteUser(id) {
	    if(confirm('确实要删除该用户吗?')) {
	$.post("<%=basePath%>user/delete.action",{"id":id},
	function(data){
	            if(data =="OK"){
	                alert("用户删除成功！");
	                window.location.reload();
	            }else{
	                alert("删除用户失败！");
	                window.location.reload();
	            }
	        });
	    }
	}
</script>
</body>
</html>